<script setup>
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { artGetListService, artDelService } from '@/api/article.js'
import { formatTime } from '@/utils/format'
import ArticleEdit from './components/ArticleEdit.vue'
const params = ref({
  pagenum: 1, //当前页
  pagesize: 5, // 每页5条数据
  cate_id: '', // 选中的分类value（id）小米手机
  state: '' //状态
})
// 文章列表数据
const articleList = ref([])
const total = ref(0)
const loading = ref(false)
//实现loading效果步骤
// 1.定义loading const loading = ref(false)
// 2.给标签绑定v-loading   <el-table v-loading="loading">
// 3.改变loading的值 loading.value = true，loading.value = false

const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()

const onSizeChange = (size) => {
  params.value.pagenum = 1 //回到第一页
  params.value.pagesize = size //每页渲染size条
  getArticleList() // 再次调用函数完成渲染
}
const onCurrentChange = (page) => {
  params.value.pagenum = page //去到第page页
  getArticleList()
}
const onSearch = () => {
  //搜索
  params.value.pagenum = 1
  getArticleList()
}

const onReset = () => {
  //重置
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
const articleEditRef = ref()
const onAddArticle = () => {
  // 发布文章
  articleEditRef.value.open({})
}

const onEditArticle = (row) => {
  //编辑
  articleEditRef.value.open(row)
}
const onDeleteArticle = async (row) => {
  //删除
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}

// 添加修改成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <!-- 传给子普通参数，动态参数是 :titel -->
    <template #extra>
      <el-button type="primary" @click="onAddArticle"> 添加文章 </el-button>
    </template>

    <!-- 主体部分 -->
    <!-- 搜索select -->
    <el-form inline>
      <el-form-item label="文章分类：">
        <channel-select v-model="params.cate_id" width="240px"></channel-select>
        <!-- 子组件与父组件的数据绑定联动，意思是父 => 子数据 子改变，父也要改变，
        但子不能直接改父的值，所以在子里面要拆开v-modul   width="240px" 属于父传子赋值，不是style的width属性-->
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select v-model="params.state" style="width: 240px">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="articleList" v-loading="loading" style="width: 100%">
      <el-table-column label="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          <!-- 解构一个row -->
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 页码 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 4, 5, 10]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <!-- @size-change="onSizeChange" 表示页数变化后会触发这个函数
      @current-change="onCurrentChange" 表示点击页面码数会触发的函数 -->
    <!-- 弹窗 -->
    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>
